package 适配器模式;

/**
 * 适配器模式
 *  应用场景:
 *      第一版的系统中一个接口，需要在新版的系统中兼容使用
 *
 */
public class AdapterPattern {


    public static void main(String[] args) {
        NewInterfaceAdapter adapter = new NewInterfaceAdapter(new OldInterfaceImpl());
        adapter.newExecute();
        NewInterfaceImpl newInterface = new NewInterfaceImpl();
        newInterface.newExecute();
    }


    /**
     * 将老接口通过构造器的方式,构造成老接口,在新接口方法中调用老接口方法
     */
    public static class NewInterfaceAdapter implements NewInterface {

        private OldInterface oldObject;

        public NewInterfaceAdapter(OldInterface oldObject) {
            this.oldObject = oldObject;
        }

        @Override
        public void newExecute() {
            //调用老接口
            oldObject.oldExecute();
        }
    }


    public static interface OldInterface {
        void oldExecute();
    }


    public static class OldInterfaceImpl implements OldInterface {

        @Override
        public void oldExecute() {
            System.out.println("老版本接口实现的功能逻辑");
        }
    }


    public static interface NewInterface {
        void newExecute();
    }


    public static class NewInterfaceImpl implements NewInterface {


        @Override
        public void newExecute() {
            System.out.println("新版本接口实现的功能逻辑");
        }
    }
}
